//
//  MapDetailViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/25.
//

#import "MapDetailViewController.h"
#import "BaseTopView.h"
#import "MapDetailTableViewCell.h"
#import "MapDeletedPopView.h"
#import <YBPopupMenu/YBPopupMenu.h>
#import "AuthorityView.h"
@interface MapDetailViewController ()<UITableViewDelegate,UITableViewDataSource,YBPopupMenuDelegate>
@property (nonatomic , strong) BaseTopView * topView;
@end

@implementation MapDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.offset = 200;
  
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"shanchu") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        NSArray * array = @[@"私人",@"删除"];
        if (weakSelf.model.isPublic) {
            array = @[@"公开",@"删除"];
        }
     YBPopupMenu * pop =  [YBPopupMenu showRelyOnView:btn titles:array icons:@[@"",@""] menuWidth:100 otherSettings:^(YBPopupMenu *popupMenu) {
            
        }];
        pop.delegate = weakSelf;
    }];
}
- (void)SQ_UIViewForSelfView{
    self.tableView.tableHeaderView = self.topView;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    WS(weakSelf);
    [self.topView setIsLikeBtnBlock:^(PointModel * _Nonnull model) {
        [weakSelf SQ_Header];
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}
- (void)SQ_Header{
    [self SQ_topView];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:self.model.userid forKey:@"pid"];
    [HttpTool post:Url17 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:self.model.userid forKey:@"pid"];
    [HttpTool post:Url17 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}
-(void)SQ_topView{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
     [dict setObject:self.model.userid forKey:@"id"];
     [HttpTool post:Url8 paramDic:dict successBlock:^(BaseModel * _Nonnull httpModel) {
         PointModel * model = [PointModel mj_objectWithKeyValues:httpModel.data];
         if (model) {
             self.model = model;
         }
         self.zx_navTitle = self.model.title;
         if (self.model.isSelf) {
             self.zx_navRightBtn.hidden = NO;
         } else {
             self.zx_navRightBtn.hidden = YES;
         }
         self.topView.model = self.model;
         if (self.isLikeBtnBlock) {
             self.isLikeBtnBlock(self.model);
         }
         [self.tableView reloadData];
     } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
         [YJProgressHUD showMsgWithoutView:httpModel.message];
     }];
}
- (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index{
    if (index ==0) {
        [self SQ_ShowAuthority];
    } else {
        [self SQ_righBtnDeleted];
    }
}
-(void)SQ_ShowAuthority{
    AuthorityView* vc = [[AuthorityView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200)];
    vc.index = self.model.isPublic;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc parentView:nil popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [vc.gongkai setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [dic setObject:@"1" forKey:@"isPublic"];
        [weakSelf SQ_Setposition:dic];
        [wk_pop dismiss];
    }];
    [vc.shiren setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [dic setObject:@"0" forKey:@"isPublic"];
        [weakSelf SQ_Setposition:dic];
        [wk_pop dismiss];
    }];
}
-(void)SQ_Setposition:(NSMutableDictionary*)dic{
    if ([self.model.userid isNotEmpty]) {
        [dic setObject:self.model.userid forKey:@"id"];
    }
    [HttpTool post:Url26 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_topView];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
    
}
-(void)SQ_righBtnDeleted{
    MapDeletedPopView * vc = [[MapDeletedPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:[Manager SQ_getCurrentViewController].view
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
      pop.hemStyle = LSTHemStyleBottom;
      [pop pop];
      LSTPopViewWK(pop)
      [pop setBgClickBlock:^{
          [wk_pop dismiss];
      }];
    [vc.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
            [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [YJProgressHUD SQ_showMsgWithoutView:@""];
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        
        [dic setObject:weakSelf.model.userid forKey:@"id"];
        [HttpTool post:Url18 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            [wk_pop dismiss];
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"删除成功")];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf SQ_popViewVC];
            });
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MapDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MapDetailTableViewCell"];
    if (!cell) {
        cell = [[MapDetailTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MapDetailTableViewCell"];
    }
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  70;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 44)];
    view.backgroundColor = UIColor.clearColor;
    UILabel * number = [[UILabel alloc]init];
    number.text = [NSString stringWithFormat:@"%@ %d",SQNSLocalString(@"点赞"),self.model.likeNum];
    number.textColor = UIColor.whiteColor;
    number.font = FONT_MEDIUM(16);
    [view addSubview:number];
    [number mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(view).offset(12);
    }];
    return view;
}

- (BaseTopView *)topView{
    if (!_topView) {
        _topView = [[BaseTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 800)];
    }
    return _topView;
}






@end
